//
//  UIButton+Tool.swift
//  ltsp
//
//  Created by jyvyi_ios on 2022/9/27.
//

import Foundation

extension UIButton{
    
    convenience init(nTitle:String,
                     sTitle:String? = nil,
                     nTitleColor:UIColor? = nil,
                     sTitleColor:UIColor? = nil,
                     font:UIFont,
                     backgroundColor:UIColor? = nil) {
        self.init(type: .custom)
        self.setTitle(nTitle, for: .normal)
        self.titleLabel?.font = font
        self.backgroundColor = backgroundColor
        
        if let nTitleColor = nTitleColor {
            self.setTitleColor(nTitleColor, for: .normal)
        }
        if let sTitleColor = sTitleColor {
            self.setTitleColor(sTitleColor, for: .selected)
        }
        if let sTitle = sTitle {
            self.setTitle(sTitle, for: .selected)
        }
    }
    
    convenience init(nImage:UIImage? = nil,sImage:UIImage? = nil) {
        self.init(type: .custom)
        
        self.setImage(nImage, for: .normal)
        self.setImage(sImage, for: .selected)
    }
    
    func setTitle(title:String? = nil,titleColor:UIColor?,font:UIFont,for state: UIControl.State){
        
        self.setTitle(title, for: state)
        self.setTitleColor(titleColor, for: state)
        self.titleLabel?.font = font
    }
    
    func setBackgroundColor(_ color: UIColor?, for state: UIControl.State,size:CGSize){
        
        let colorImage = UIImageMake(With: color, size: size)
        self.setBackgroundImage(colorImage, for: state)
    }
}
